#include "cell_record.h"
#include "ui_cell_record.h"
#include "lib/sqlmgr.h"
#include "usrconfirm.h"

#include <QMessageBox>

Cell_Record::Cell_Record(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Cell_Record)
{
    ui->setupUi(this);
    ui->tableView->setModel(&m_model);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置图表不可编辑
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows); //只允许选中行
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); //最多选中一行
}

void Cell_Record::initPage(QString strCondition)
{
    //查询数据库并显示
    auto l = SqlMgr::getInstance()->getRecord(strCondition);
    //将数据放入model以在界面中显示
    m_model.clear();
    m_model.setHorizontalHeaderLabels(QStringList{"id", "用户id", "书籍id", "起始日期", "结束日期"});
    for(int i = 0; i < l.size(); ++i)
    {
        QList<QStandardItem*> items;
        for(int j = 0; j < l[i].size(); ++j) {
            items.append(new QStandardItem(l[i][j]));
        }
        m_model.appendRow((items));
    }
}

Cell_Record::~Cell_Record()
{
    delete ui;
}

void Cell_Record::on_btn_clear_clicked()
{
    if(usrConfirm(CLR)) {
        SqlMgr::getInstance()->clearRecord();
        initPage();
    }
    else { return; }
}

void Cell_Record::on_le_search_textChanged(const QString &arg1)
{
    initPage(arg1);
}

